package com.paomi.client.activity.person;

import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.activity.person.Adapter.MyCollectionAdapter;
import com.paomi.client.activity.person.View.CollectionView;
import com.paomi.client.activity.person.presenter.CollectionPre;
import com.paomi.client.base.BaseMvpActivity;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.CollectionListInfo;
import com.paomi.client.bean.CpllectionInfo;
import com.paomi.client.fragment.collection.CollectNewsFragment;
import com.paomi.client.fragment.collection.CollectShopFragment;
import com.paomi.client.fragment.collection.CollectShowFragment;
import com.paomi.client.refresh.SwipeToLoadHelper;
import com.paomi.client.utils.ToastUtil;
import com.paomi.client.utils.Util;
import com.paomi.client.view.LoadingDialog;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyCollectionActivity extends BaseMvpActivity<CollectionView, CollectionPre> implements CollectionView, SwipeToLoadHelper.LoadMoreListener {
//    @BindView(R.id.recycler_collect)
//    RecyclerView recyclerView;
    MyCollectionAdapter adapter;
    @BindView(R.id.ll_none)
    LinearLayout llNone;
    @BindView(R.id.collect_show)
    RadioButton collectShow;
    @BindView(R.id.collect_shop)
    RadioButton collectShop;
    @BindView(R.id.collect_information)
    RadioButton collectInformation;
    @BindView(R.id.radio_lay)
    RadioGroup radioLay;
    @BindView(R.id.collect_vp)
    ViewPager collectVp;
    private List<CpllectionInfo> list = new ArrayList<>();
    //添加上拉加载
//    private AdapterWrapper mAdapterWrapper;
//    private SwipeToLoadHelper mLoadMoreHelper;
    private int pageNum = 1;
    private CollectShowFragment collectShowFragment;//演出列表
    private CollectShopFragment collectShopFragment;//周边列表
    private CollectNewsFragment collectNewsFragment;//资讯列表
    private List<Fragment> fragmentList;

    @NonNull
    @Override
    public CollectionPre createPresenter() {
        return new CollectionPre();
    }

    @Override
    protected String setTitle() {
        return getResources().getString(R.string.string_my_collection);
    }

    @Override
    protected void showBack(TextView button_goback) {
        button_goback.setVisibility(View.VISIBLE);
        button_goback.setTypeface(iconfont);
        button_goback.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        button_goback.setTextColor(getResources().getColor(R.color.main_color));
        button_goback.setText(getResources().getString(R.string.back_normal) + getResources().getString(R.string.back));
    }

    @NonNull
    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_collection;
    }


    @Override
    protected void initEvent() {
        collectShowFragment = new CollectShowFragment();
        collectShopFragment = new CollectShopFragment();
        collectNewsFragment = new CollectNewsFragment();
        fragmentList = new ArrayList<>();
        fragmentList.add(collectShowFragment);
        fragmentList.add(collectShopFragment);
        fragmentList.add(collectNewsFragment);
        collectVp.setAdapter(new ListPageAdapter(getSupportFragmentManager(),fragmentList));
        collectVp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        collectShow.setChecked(true);
                        break;
                    case 1:
                        collectShop.setChecked(true);
                        break;
                    case 2:
                        collectInformation.setChecked(true);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        collectShow.setOnClickListener(addClick);
        collectShop.setOnClickListener(addClick);
        collectInformation.setOnClickListener(addClick);

//        recyclerView.setLayoutManager(new LinearLayoutManager(this));
//        adapter = new MyCollectionAdapter(this);
//        mAdapterWrapper = new AdapterWrapper(adapter);
//        mLoadMoreHelper = new SwipeToLoadHelper(recyclerView, mAdapterWrapper);
//        mLoadMoreHelper.setLoadMoreListener(this);
//        recyclerView.setAdapter(mAdapterWrapper);
//        getPresenter().getFavorList("0", pageNum);
//
//        adapter.doClick(new MyCollectionAdapter.doClick() {
//            @Override
//            public void delete(String id, int pos) {
//                Map<String, String> map = new HashMap<>();
//                map.put("typeId", id);
//                map.put("type", "0");
//                getPresenter().removeCollection(map, pos);
//            }
//        });
    }
    View.OnClickListener addClick=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.collect_show://演出
                    collectVp.setCurrentItem(0);
                    break;
                case R.id.collect_shop://周边
                    collectVp.setCurrentItem(1);
                    break;
                case R.id.collect_information://资讯
                    collectVp.setCurrentItem(2);
                    break;
            }
        }
    };


    @Override
    public void showCollection(CollectionListInfo info) {
        LoadingDialog.get().hideLoading();
        if (info.getReturnData().getData().size() > 0) {
            if (pageNum == 1) {
                list.clear();
            }
            if (list.size() > 0) {
                boolean isData = false;
                List<CpllectionInfo> listData = info.getReturnData().getData();
                //判断是否有重复数据
                for (CpllectionInfo cpllectionInfo : listData) {
                    for (int i = 0; i < list.size(); i++) {
                        if (cpllectionInfo.getTypeId() == list.get(i).getTypeId()) {
                            isData = false;
                            break;
                        } else {
                            isData = true;
                        }

                    }
                }
                if (isData) {
                    list.addAll(info.getReturnData().getData());
                } else {
                    ToastUtil.getToastEmail().ToastShow(this, null, 1, R.layout.toast_view, getResources().getString(R.string.data_null));
                }
            } else {
                list.addAll(info.getReturnData().getData());
            }
            adapter.setData(list);
        } else {
            llNone.setVisibility(View.VISIBLE);
        }
//        if (list.size() %Constants.PAGE_SIZE != 0) {//移除FooterView
//            mAdapterWrapper.setLoadItemVisibility(false);
//        }else{
//            mAdapterWrapper.setLoadItemVisibility(true);
//        }
//        mLoadMoreHelper.setLoadMoreFinish();
//        mAdapterWrapper.notifyDataSetChanged();
    }

    @Override
    public void removeSuccess(BaseJSON json, int pos) {
        if (json.returnCode.equals("0000")) {
            Util.toast(this, "取消收藏");
            list.remove(pos);
            adapter.notifyDataSetChanged();
//            mAdapterWrapper.notifyDataSetChanged();
        } else {
            Util.toast(this, json.returnMessage);
        }
    }

    @Override
    public void onLoad() {
        pageNum++;
        getPresenter().getFavorList("0", pageNum);
//        if(list.size()%Constants.PAGE_SIZE==0){//是10的倍数
//
//        }else{
//            mAdapterWrapper.setLoadItemVisibility(false);
//            mLoadMoreHelper.setLoadMoreFinish();
//        }
    }
    class  ListPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> list;

        public ListPageAdapter(FragmentManager fm,List<Fragment> list){
            super(fm);
            this.list=list;
        }

        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

}
